package ftdpc.application;

import fdupc.http.ResultBean;
import ftdpc.TaskDispatchBusinessManager;
import ftdpc.bean.TaskAttribute;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 类描述:  Redis管理交互
 *
 * @author 王雷
 * @version 1.0.0
 * @date 2022/6/22 0022 下午 3:39
 */
@Slf4j
public class DispatchTaskController implements IDispatchTaskController {

    private final TaskDispatchBusinessManager taskBusinessManager;

    public DispatchTaskController(TaskDispatchBusinessManager taskBusinessManager) {
        this.taskBusinessManager = taskBusinessManager;
    }

    /**
     * 功能描述：获取所有任务描述属性
     *
     * @date : 2022/6/22 0022 下午 3:41
     */
    @Override
    public ResultBean giveTaskAttributeList() {
        return ResultBean.factorySuccess(new ArrayList<TaskAttribute>() {
            private static final long serialVersionUID = -5884726260967476537L;

            {
                final Map<String, TaskAttribute> taskAttributeMap = taskBusinessManager.giveTaskAttributeMap();
                if (!taskAttributeMap.isEmpty()) {
                    taskAttributeMap.forEach((taskKey, taskAttribute) -> {
                        add(taskAttribute);
                    });
                }
            }

        });
    }

    /**
     * 功能描述：根据标识获取任务描述属性
     *
     * @param taskKey 配置标识
     * @date : 2022/6/22 0022 下午 3:41
     */
    @Override
    public ResultBean giveTaskAttributeItem(String taskKey) {
        final TaskAttribute taskAttribute = taskBusinessManager.giveTaskAttributeMap().get(taskKey);
        if (taskAttribute != null) {
            return ResultBean.factorySuccess(taskAttribute);
        }
        return ResultBean.factoryError("没有对应的配置");
    }

    /**
     * 功能描述：获取任务日志信息
     *
     * @param taskKey 配置标识
     * @date : 2022/6/22 0022 下午 3:41
     */
    @Override
    public ResultBean giveTaskLog(String taskKey) {
        try {
            return ResultBean.factorySuccess(taskBusinessManager.giveTaskLog(taskKey));
        }catch (Exception e) {
            log.error("获取任务日志信息异常", e);
            return ResultBean.factoryError("获取任务日志信息异常");
        }
    }

    /**
     * 功能描述：修改配置属性
     *
     * @param taskAttribute 配置属性
     * @date : 2022/9/14 0014 下午 3:24
     */
    @Override
    public ResultBean updateAttribute(TaskAttribute taskAttribute) {
        try {
            taskBusinessManager.updateAttribute(taskAttribute);
            return ResultBean.factorySuccess();
        } catch (Exception e) {
            log.error("将配置存入配置处理异常", e);
            return ResultBean.factoryError(e.getMessage());
        }
    }

    /**
     * 功能描述：将属性存入配置
     *
     * @param taskKey 配置标识
     * @date : 2022/9/14 0014 下午 1:01
     */
    @Override
    public ResultBean putToConfig(String taskKey) {
        try {
            taskBusinessManager.putTaskAttributeToConfig(taskKey);
            return ResultBean.factorySuccess();
        } catch (Exception e) {
            log.error("将配置存入配置处理异常", e);
            return ResultBean.factoryError();
        }
    }

    private final ReentrantLock lock = new ReentrantLock();

    /**
     * 功能描述：线程启动或关闭
     *
     * @param taskKey 配置标识
     * @date : 2022/9/14 0014 下午 1:01
     */
    @Override
    public ResultBean taskStartOrStop(String taskKey) {
        lock.lock();
        try {
            final TaskAttribute taskAttribute = taskBusinessManager.giveTaskAttributeMap().get(taskKey);
            if(taskAttribute==null){
                return ResultBean.factoryError("不存在的属性信息");
            }
            if (taskAttribute.getTaskIsItEffective() == 0) {
                taskBusinessManager.taskStart(taskKey);
            } else {
                taskBusinessManager.taskStopByKey(taskKey);
            }
            return ResultBean.factorySuccess();
        } catch (Exception e) {
            log.error("线程启动处理异常", e);
            return ResultBean.factoryError();
        } finally {
            lock.unlock();
        }
    }
}
